package com.foreveross.chameleonsdk;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.apache.http.util.EncodingUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.util.Log;

import com.darin.cl.task.CLTaskManager;
import com.foreveross.URL;
import com.foreveross.immodule.R;
import com.foreveross.util.PropertiesUtil;

/**
 * 通过ChameleonSDK.getCApplication()获取实例
 * @author Jianfeng.Lao
 * 
 */
public class CApplication {

	public static final String CMODULE_PUSH = "CpushModule";
	public static final String CMODULE_MANAGER = "CmanagerModule";
	private static final String TAG = "CApplication";

	private HashMap<String, CModule> modules = new HashMap<String, CModule>();
	private Context mContext;
	private CLTaskManager mClTaskManager;// 共享线程池,方法其它模块使用
	private PropertiesUtil cubeProerties;
	/**
	 * 
	 * @param context
	 * @param chameleonJson 模块配置文件位置
	 * @param chameleonPropreties 后台/其它配置文件位置
	 */
	public CApplication(Context context, String chameleonJson,String chameleonPropreties) {
		mContext = context;
		InputStream json;
		InputStream propreties;
		try {
			json = mContext.getAssets().open(chameleonJson);
			propreties = mContext.getAssets().open(chameleonPropreties);
			cubeProerties =PropertiesUtil.readProperties(propreties);
			URL.initUrl(mContext, cubeProerties);
			initModulesConfig(json);
			mClTaskManager = new CLTaskManager(128, 8, 8);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}


	public void onConfigurationChanged(Configuration newConfig) {
		for (CModule m : modules.values()) {
			m.onConfigurationChanged(newConfig);
		}
	}

	public void onCreate() {
		for (CModule m : modules.values()) {
			m.onCreate(m);
		}
	}

	public void onExit() {
		for (CModule m : modules.values()) {
			m.onExit(m);
		}
	}

	public void onLowMemory() {
		for (CModule m : modules.values()) {
			m.onLowMemory();
		}
	}

	public void onTerminate() {
		for (CModule m : modules.values()) {
			m.onTerminate();
		}
		if (mClTaskManager != null) {
			mClTaskManager.destory();
		}
	}

	public CModule getModule(String identifier) {
		return modules.get(identifier);
	}

	public HashMap<String, CModule> getModules() {
		return modules;
	}

	public void setModules(HashMap<String, CModule> modules) {
		this.modules = modules;
	}

	public Context getContext() {
		return mContext;
	}

	protected void initModulesConfig(InputStream is) {

		String result = "";
		try {
			int lenght = is.available();
			byte[] buffer = new byte[lenght];
			is.read(buffer);
			result = EncodingUtils.getString(buffer, "ENCODING");
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			JSONObject json = new JSONObject(result);
			JSONArray jay = json.getJSONArray("modules");
			for (int i = 0; i < jay.length(); i++) {
				JSONObject jb = (JSONObject) jay.get(i);
				String identifier = (String) jb.get("identifier");
				String name = (String) jb.get("name");
				String packagename = (String) jb.get("package");
				boolean firstStart = jb.getBoolean("firststart");
				Class<?> clazz = null;
				try {
					clazz = Class.forName(packagename + "." + name);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} finally {
					if (clazz != null) {
						CModule cModule = (CModule) clazz.newInstance();
						cModule.setCApplication(this);
						cModule.setFirstStart(firstStart);
						cModule.setPackageName(packagename);
						cModule.setName(name);
						modules.put(identifier, cModule);
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void startFirstModule() {
		for (CModule module : modules.values()) {
			String activity = module.getActivity();
			if (module.isFirstStart()) {
				Intent intent = new Intent();
				intent.setClassName(mContext, activity);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mContext.startActivity(intent);
			}
		}
	}

	public CLTaskManager getClTaskManager() {
		return mClTaskManager;
	}

	public PropertiesUtil getCubePropertie() {
		return cubeProerties;
	}

}
